package bean.Resource;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.List;

/**
 * 使用Spring提供的Resource接口访问网络资源
 */
public class UrlResourceTest {
    public static void main(String[] args) {
        try {
            UrlResource url = new UrlResource("file:/home/hadoop/derby.log");
            System.out.println("FileName "+url.getFilename());
            System.out.println("Description: "+url.getDescription());
            System.out.println(url.exists());

            showResourceFile(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //打印Resource接口对应的InputStream
    public static void showResourceFile(Resource url){
        try(
                InputStream is = url.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr)
        ){

            String line;
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
